<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ################


if(!isset($_SESSION['JFX_Module_Vps_NewPackageData']) || !is_array($_SESSION['JFX_Module_Vps_NewPackageData'])){

    $_SESSION['JFX_Module_Vps_NewPackageData'] = array(
        1 => array(), // package details
        2 => array(), // scan folder
        3 => array(), // file updates
        4 => array(), // db updates
        5 => array() // eval code
    );

}

$pData =& $_SESSION['JFX_Module_Vps_NewPackageData'];




if(Antz_IntelliForm::submitted('save-new-version')){
    // process

    $packageid = $pData[1]['package_id'];
    $version = $pData[1]['version'];

    $dbParams = array(
        'package_id' => $packageid,
        'version' => $version,
        'previous_version' => $pData[4]['previous_version'],
        'eval_code' => $pData[4]['eval_code'],
        'change_log' => $pData[1]['change_log']
    );

    $versionid = $DB->insert($CONFIG->dbprefix.'vms_versions', $dbParams);

    $this->updateContentLang('change_log', $versionid, $pData[1]['change_log']);

    // precautionary idea
    $DB->delete($CONFIG->dbprefix.'vms_files', "package_id = '{$packageid}' AND version_id = '{$versionid}'");

    // copy the files
    foreach($pData[3] as $k=>$fileData){

        $dbParams = array(
            'filename' => $fileData['filename'],
            'file_type' => $fileData['type'],
            'package_id' => $packageid,
            'version_id' => $versionid,
            'update_action' => $fileData['action']
        );
        
        $fileid = $DB->insert($CONFIG->dbprefix.'vms_files', $dbParams);

        if(file_exists($this->dirname.'/files/'.$fileid.'.vmsf')) unlink($this->dirname.'/files/'.$fileid.'.vmsf');
        $fh = fopen($this->dirname.'/files/'.$fileid.'.vmsf', 'w');
        fwrite($fh, $fileData['file_contents']);
        fclose($fh);

    }

    $pData = array(
        array(),
        array(),
        array(),
        array()
    );
    unset($_SESSION['JFX_Module_Vms_ScanFilesTemp']);
    JFX::addSuccess('Version has been added!');
    JFX::redirect($this->adminUrl);
}






ob_start();

echo '<table id="vms-new-version"><tbody>';

$clearSteps = array();
for($i=1; $i<=5; $i++) $clearSteps[$i] = false;

echo '<tr><th>Package Details</th>';

// package details
if(count($pData[1])>0 && get('reset')!=1){
    $clearSteps[1] = true;
    echo '<td>
    <a href="javascript: void(null);" onclick="$(\'.toggle-package-details\').toggle(\'slow\')">Toggle</a></td>
    <td><a href="'.$this->adminUrl.'/new-version/?reset=1">Reset</a></td>
    <tr><td colspan="3"><div style="display: none;" class="toggle-package-details">';
    include('new-version/package-details.php');
    echo '</div></td></tr>';
}else{
    echo '<td><img src="'.$this->imageUrl.'/vtab-open.gif" alt="Open" /></td><td>&nbsp;</td></tr>';
    echo '<tr><td colspan="3">';
    include('new-version/package-details.php');
    echo '</td></tr>';
};

echo '<tr><th>Select Files</th>';

if(($clearSteps[1] && count($pData[2])>0) && get('reset')!=2){
    $clearSteps[2] = true;

    echo '<td>
    <a href="javascript: void(null);" onclick="$(\'.toggle-scan-folder\').toggle(\'slow\')">Toggle</a></td>
    <td><a href="'.$this->adminUrl.'/new-version/?reset=2">Reset</a></td></tr>
    <tr><td colspan="3"><div style="display: none;" class="toggle-scan-folder">';
    include('new-version/scan-folder.php');
    echo '</div></td></tr>';

}else if($clearSteps[1]){
    echo '<td><img src="'.$this->imageUrl.'/vtab-open.gif" alt="Open" /></td><td>&nbsp;</td></tr>';
    echo '<tr><td colspan="3"><div align="left">';
    include('new-version/scan-folder.php');
    echo '</div></td></tr>';
}else{
    echo '</td><td>&nbsp;</td></tr><tr><td colspan="3">&nbsp;</td></tr>';
}

echo '<tr><th>File Updates</th>';

if(($clearSteps[1] && $clearSteps[2] && count($pData[3])>0) && get('reset')!=3){
    $clearSteps[3] = true;

    echo '<td>
    <a href="javascript: void(null);" onclick="$(\'.toggle-file-updates\').toggle(\'slow\')">Toggle</a></td>
    <td><a href="'.$this->adminUrl.'/new-version/?reset=3">Reset</a></td></tr>
    <tr><td colspan="3"><div style="display: none;" class="toggle-file-updates">';
    include('new-version/file-updates.php');
    echo '</div></td></tr>';
}else if($clearSteps[1] && $clearSteps[2]){
    echo '<td><img src="'.$this->imageUrl.'/vtab-open.gif" alt="Open" /></td><td>&nbsp;</td></tr>';
    echo '<tr><td colspan="3"><div align="left">';
    include('new-version/file-updates.php');
    echo '</div></td></tr>';
}else{
    echo '</td><td>&nbsp;</td></tr><tr><td colspan="3">&nbsp;</td></tr>';
}

echo '<tr><th>Eval Code</th>';

if(($clearSteps[1] && $clearSteps[2] && $clearSteps[3] && count($pData[4])>0) && get('reset')!=4){
    $clearSteps[4] = true;
    echo '<td>
    <a href="javascript: void(null);" onclick="$(\'.toggle-eval-code\').toggle(\'slow\')">Toggle</a></td>
    <td><a href="'.$this->adminUrl.'/new-version/?reset=4">Reset</a></td></tr>
    <tr><td colspan="3"><div style="display: none;" class="toggle-eval-code">';
    include('new-version/eval-code.php');
    echo '</div></td></tr>';
}else if($clearSteps[1] && $clearSteps[2] && $clearSteps[3]){
    echo '<td><img src="'.$this->imageUrl.'/vtab-open.gif" alt="Open" /></td><td>&nbsp;</td></tr>';
    echo '<tr><td colspan="3"><div align="left">';
    include('new-version/eval-code.php');
    echo '</div></td></tr>';
}else{
    echo '</td><td>&nbsp;</td></tr><tr><td colspan="3">&nbsp;</td></tr>';
}


if($clearSteps[4]){
    // now we can commit the changes to the database
    $SMARTY->assign('vData', $pData);
    $saveButton = $this->fetchTemplate('new-version/confirm.tpl');
    $saveButton .= '<div class="vms-new-version-button">
<form method="post">'.Antz_IntelliForm::seed('save-new-version').'
<input type="submit" value="Save This Version" />
</form>
</div>';
}

echo '</tbody></table>';

$output = ob_get_contents();
ob_end_clean();












echo '<h1>Create New Version</h1>';

?>
<p>Code is managed in Packages. Each package has a known set of files. Version
Management facilitates the process of recording changes to files and distributing
updates.</p>
<p>Any time after making significant changes to your code you should make a new
version and note any changes since the last version in the change log.</p>
<p>Follow these four steps to create a new version which you can afterwards publish
 for your subscribers.</p>
<?php

echo $output;

echo $saveButton;





